package com.fy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fy.common.constants.Constants;
import com.fy.common.annotation.GlobalInterceptor;
import com.fy.mapper.AnnouCheckMapper;
import com.fy.pojo.entity.AnnouCheck;
import com.fy.pojo.entity.Announcement;
import com.fy.pojo.vo.other.DurationTimeVO;
import com.fy.pojo.vo.user.LoginUserVO;
import com.fy.pojo.vo.other.SystemSpaceVO;
import com.fy.pojo.vo.announcement.UserAnnouncementVO;
import com.fy.service.AnnouCheckService;
import com.fy.service.AnnouncementService;
import com.fy.service.SystemService;
import com.fy.utils.DateFormatter;
import com.fy.utils.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static cn.hutool.core.bean.BeanUtil.copyProperties;

@Slf4j
@RestController
@RequestMapping("/system")
public class SystemController {

    @Autowired
    private SystemService systemService;

    @Autowired
    private AnnouCheckService annouCheckService;

    @Autowired
    private AnnouCheckMapper annouCheckMapper;

    /**
     * 获取OSS空间使用情况
     *
     * @return
     */
    @PostMapping("/getSystemSpace")
    // @GlobalInterceptor
    public Result getSystemSpace(HttpSession session) {
        // SystemSpaceVO systemSpaceVo=systemService.getSystemSpace(session);
        SystemSpaceVO systemSpaceVo = new SystemSpaceVO();
        systemSpaceVo.setSystemUseSpace("100");
        systemSpaceVo.setTitle("系统空间使用情况");
        systemSpaceVo.setOssUseSpace("100");

        return Result.success(systemSpaceVo);
    }

    /**
     * 获取扩容套餐
     *
     * @return
     */
//    @PostMapping("/getExpansionType")
//    @GlobalInterceptor
//    public Result getExpansionType() {
//        return Result.success(systemService.getExpansionType());
//    }

    /**
     * 扩容
     *
     * @param httpSession
     * @param type
     * @return
     */
    @PostMapping("/doExpansion")
    @GlobalInterceptor
    public Result doExpansion(HttpSession httpSession, @RequestParam("type") Integer type) {
        LoginUserVO loginUserVO = (LoginUserVO) httpSession.getAttribute(Constants.SESSION_KEY);
        systemService.doExpansion(loginUserVO.getNickName(), type);
        return Result.success();
    }

    /**
     * 获取首页系统功能使用情况
     *
     * @return
     */
    @PostMapping("/getSystemFunction")
    @GlobalInterceptor
    public Result<?> getSystemFunction() {
        return Result.success(systemService.getLast7DaysUsage());
    }

    /**
     * 获取登录用户分布统计
     *
     * @return
     */
    @PostMapping("/getRegionalDistribution")
    @GlobalInterceptor
    public Result getRegionalDistribution() {
        return Result.success(systemService.getRegionalDistribution());
    }

    /**
     * 获取登录时间统计
     *
     * @return
     */
    @PostMapping("/getDurationTime")
    @GlobalInterceptor
    public Result<?> getDurationTime() {
        List<DurationTimeVO> durationTimes = systemService.getDurationTime();
        Map<String, Object> map = new HashMap<>();
        map.put("durationTimes", durationTimes);
        return Result.success(map);
    }

    /**
     * 获取系统工具
     *
     * @return
     */
    @PostMapping("/getToolsList")
    @GlobalInterceptor
    public Result getToolsList() {
        return Result.success(systemService.getToolsList());
    }

    @Autowired
    private AnnouncementService announcementService;

    @PostMapping("/announcements")
    @GlobalInterceptor
    public Result<List<UserAnnouncementVO>> getAnnouncements(HttpSession httpSession) {
        LoginUserVO loginUserVO = (LoginUserVO) httpSession.getAttribute(Constants.SESSION_KEY);
        Long userId = loginUserVO.getId();
        // 获取所有有效的公告
        LambdaQueryWrapper<Announcement> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Announcement::getStatus, 1) // 状态为有效
                .orderByDesc(Announcement::getCreateTime); // 按创建时间降序

        List<Announcement> announcements = announcementService.list(wrapper);

        // 转换为VO对象
        List<UserAnnouncementVO> announcementVOs = announcements.stream()
                .map(announcement -> {
                    UserAnnouncementVO vo = new UserAnnouncementVO();
                    LambdaQueryWrapper<AnnouCheck> queryWrapper=new LambdaQueryWrapper<>();
                    queryWrapper.eq(AnnouCheck::getUserId,userId);
                    queryWrapper.eq(AnnouCheck::getAnnouId,announcement.getId());
                    Long count = annouCheckMapper.selectCount(queryWrapper);
                    if (count == null || count <= 0){
                        vo.setReadStatus(0);
                    }else {
                        vo.setReadStatus(1);
                    }
                    copyProperties(announcement, vo);
                    vo.setShowTime(DateFormatter.getTimestampStringByDate(announcement.getShowTime()));
                    return vo;
                })
                .collect(Collectors.toList());

        return Result.success(announcementVOs);
    }

    /**
     * 获取公告详情读公告:/readAnnouncement
     *
     * @param session
     * @param id
     * @return /system
     */
    @PostMapping("/readAnnouncement")
    @GlobalInterceptor
    public Result readAnnouncement(HttpSession session,@RequestParam("id") Long id) {
        LoginUserVO loginUserVO = (LoginUserVO) session.getAttribute(Constants.SESSION_KEY);
        UserAnnouncementVO userAnnouncementVO = annouCheckService.readAnnouncement(loginUserVO.getId(), id);
        return Result.success(userAnnouncementVO);
    }


}
